﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructure
{
     public enum GState
     {
         Unvisited,
         Visited,
         Visisting
     }

    public class GNode
    {
        public GState State;
        private int data;
        public int Data
        {
            get { return data; }
            set { data = value; }
        }

        public GNode(int data)
        {
            this.data = data;
        }
        private List<GNode> ajacents = new List<GNode>();
        public void AddAjacents(GNode n)
        {
            ajacents.Add(n);
        }
        public List<GNode> Ajacents
        {
            get
            {
                return ajacents;
            }
        }
    }
    public class Graphs
    {
        public Graphs()
        {

        }
        List<GNode> nodes = new List<GNode>();

        public List<GNode> Nodes
        {
            get { return nodes; }
            set { nodes = value; }
        }
        public Graphs(List<GNode> list)
        {
            nodes = list;
        }
        public static bool IsRoute(Graphs g, GNode start, GNode end)
        {
            if (g == null) return false;
            foreach (GNode item in g.Nodes)
            {
                item.State = GState.Unvisited;
            }

            Stack<GNode> inVisiting = new Stack<GNode>();
            start.State = GState.Visisting;
            inVisiting.Push(start);
            GNode current;
            while (inVisiting.Count > 0)
            {
                current = inVisiting.Pop();
                Console.WriteLine(current.Data);
                if (current != null)
                {
                    foreach (GNode node in current.Ajacents)
                    {
                        if (node.State == GState.Unvisited)
                            if (node.Data == end.Data)
                            {
                                return true;
                            }
                            else
                            {
                                node.State = GState.Visisting;
                                inVisiting.Push(node);
                            }
                    }
                    current.State = GState.Visited;
                }

            }
            return false;
        }
    }
}
